<?php

namespace app\admin\model;

use think\Model;

class MarketingUsers extends Model
{
    # 设置当前模型对应的完整数据表名称
    protected $name = 'marketing_users_cp';

    /**
     * 活动用户列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getMarketingUsers($where, $page, $pagesize)
    {
        $data = self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->join('users_cate c', 'm.uid = c.uid', 'left')
            ->join('identity i', 'c.identity_id = i.id', 'left')
            ->field('m.*,u.nickname,u.headimg,u.role,u.associate,i.name as iname')
            ->where($where)
            ->page($page, $pagesize)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 活动用户统计
     * @param $where
     * @return int
     */
    public static function countMarketingUsers($where)
    {
        return self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->join('users_cate c', 'm.uid = c.uid', 'left')
            ->join('identity i', 'c.identity_id = i.id', 'left')
            ->where($where)
            ->count();
    }

    /**
     * 活动金额累计
     * @param $where
     * @param string $field
     * @return float
     */
    public static function sumMarketingUsersPrice($where = [], $field = 'money')
    {
        return self::where($where)->sum($field);
    }

    /**
     * 过去7天活动金额
     * @param $where
     * @param $startTime
     * @param $endTime
     * @param string $field
     * @return array
     */
    public static function getMarketingUsersPrice($where, $startTime, $endTime, $field = 'money')
    {
        $data = self::fieldRaw("SUM($field) total,create_time")
            ->where($where)
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->group('create_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 所有端活动用户总数
     * @param $where
     * @return int
     */
    public static function totalAllMarketingUsers($where)
    {
        return self::where($where)->count('distinct uid');
    }

    /**
     * 指定端活动用户总数
     * @param $where
     * @return mixed
     */
    public static function totalOtherMarketingUsers($where)
    {
        return self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->where($where)
            ->count('distinct m.uid');
    }

    /**
     * 今日活动用户
     * @param $where
     * @return int
     */
    public static function todayMarketingUsers($where)
    {
        return self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->whereDay('m.create_time')
            ->where($where)
            ->count('distinct m.uid');
    }

    /**
     * 昨日活动用户
     * @param $where
     * @return int
     */
    public static function yesTodayMarketingUsers($where)
    {
        return self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->whereDay('m.create_time', 'yesterday')
            ->where($where)
            ->count('distinct m.uid');
    }

    /**
     * 本月活动用户
     * @param $where
     * @return int
     */
    public static function monthMarketingUsers($where)
    {
        return self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->whereMonth('m.create_time')
            ->where($where)
            ->count('distinct m.uid');
    }

    /**
     * 其他活动用户
     * @return array
     */
    public static function otherMarketingUsers()
    {
        $data = self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->fieldRaw("COUNT(distinct m.uid) usersNums,u.associate")
            ->group('u.associate')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 本月活动用户（折线图）
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getMarketingUsersLine($startTime, $endTime)
    {
        $data = self::fieldRaw("COUNT(distinct uid) total,create_time")
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->group('create_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 下单地区分布
     * @param $where
     * @param int $offset
     * @param int $length
     * @return array
     */
    public static function getMarketingOrderArea($where, $offset = 0, $length = 6)
    {
        $data = self::fieldRaw("COUNT(distinct uid) nums,area as name")
            ->where($where)
            ->group('area')
            ->order('nums', 'desc')
            ->limit($offset, $length)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 统计下单地区
     * @param $where
     * @return int
     */
    public static function totalMarketingOrderArea($where = [])
    {
        return self::where($where)->count('distinct area');
    }

    /**
     * 下单身份数据
     * @param $where
     * @return array
     */
    public static function getMarketingOrderIdentity($where)
    {
        $data = self::alias('m')
            ->where($where)
            ->distinct(true)
            ->field('m.uid')
            ->select();

        return $data ? $data->toArray() : [];
    }
}